package org.payeco.risk.controller;

import java.util.List;

import org.payeco.risk.dao.PayecoRiskRuleRepository;
import org.payeco.risk.dao.PayecoRuleModularRepository;
import org.payeco.risk.entity.PayecoRiskRule;
import org.payeco.risk.entity.PayecoRuleModular;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yps
 *
 */
@RestController
public class PayecoModularController {
    @Autowired
    PayecoRuleModularRepository modularRepository ;

    @RequestMapping("/saveModular")
    public PayecoRuleModular save(String modularNo, String modularName) {
    	PayecoRuleModular modular = modularRepository.save(new PayecoRuleModular(modularNo, modularName));
        return modular;
    }
 
    @RequestMapping("/queryModulars")
    public List<PayecoRuleModular> queryRules(String state) {
        List<PayecoRuleModular> modulars = modularRepository.findByState(state);
        return modulars;
    }

    @RequestMapping("/queryByModularNo")
    public PayecoRuleModular q3(String modularNo) {
    	PayecoRuleModular modular = modularRepository.findByModularNo(modularNo);
        return modular;
    }

    @RequestMapping("/modulars_sort")
    public List<PayecoRuleModular> sort() {
        List<PayecoRuleModular> modulars = modularRepository.findAll(new Sort(Sort.Direction.ASC, "priority"));
        return modulars;
    }
    @RequestMapping("/modulars_page")
    public Page<PayecoRuleModular> page(int page,int size){
        Page<PayecoRuleModular> all = modularRepository.findAll(new PageRequest(page, size));
        return all;
    }
    @RequestMapping("/modulars_all")
    public List<PayecoRuleModular> all(){
        return modularRepository.findAll();
    }
}
